package org.room402.chat.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Slf4j
@Component
public class SimpleWebsocketHandler implements WebSocketHandler {
    @Override
    public Mono<Void> handle(WebSocketSession session) {
        Flux<WebSocketMessage> msg = session.receive()
                .map(WebSocketMessage::getPayloadAsText)
                .map(x -> {
                    log.info("【收到消息】{}", x);
                    return "已收到: " + x;
                })
                .map(session::textMessage);

        return session.send(msg);
    }
}
